package org.javaboy.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.javaboy.ai.domain.Category;
import org.javaboy.ai.mapper.CategoryMapper;
import org.javaboy.ai.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author javaboy
 * @since 2024-07-03
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<Category> getAllCategories() {
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getDelFlag, 1);
        qw.orderByAsc(Category::getOrderNum);
        qw.select(Category::getText, Category::getSrc, Category::getName, Category::getEnable);
        return list(qw);
    }

    @Override
    public List<Category> selectCategoryList(Category category) {
        return categoryMapper.selectCategoryList(category);
    }

    @Override
    public boolean checkNameExists(Category category) {
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getDelFlag, 1);
        qw.eq(Category::getName, category.getName());
        if (category.getId() != null) {
            qw.ne(Category::getId, category.getId());
        }
        return exists(qw);
    }

    @Override
    public boolean deleteCategory(Long[] categoryIds) {
        LambdaUpdateWrapper<Category> uw = new LambdaUpdateWrapper<>();
        uw.in(Category::getId, categoryIds);
        uw.set(Category::getDelFlag, 0);
        return update(uw);
    }

    @Override
    public List<String> getAllCategoryName() {
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getDelFlag, 1);
        qw.select(Category::getName);
        List<Category> list = list(qw);
        return list.stream().map(Category::getName).collect(Collectors.toList());
    }
}
